package org.neptune.service.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.neptune.api.entity.AesKeyBean;
import org.neptune.api.entity.SeckillUser;
import org.neptune.api.entity.SeckillUserExample;
import org.neptune.api.request.RegisterRequest;
import org.neptune.common.constant.NeptuneConstant;
import org.neptune.common.response.ResponseBean;
import org.neptune.common.util.AESUtils;
import org.neptune.common.util.RandomUtils;
import org.neptune.common.util.SecurityKeyUtil;
import org.neptune.service.dao.SeckillUserMapper;
import org.neptune.service.redis.RedisRepository;
import org.neptune.service.redis.RedissonUtil;
import org.neptune.service.service.UserRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class UserRegisterServiceImpl implements UserRegisterService {

	@Autowired
	private RedisRepository redisRepository;

	@Autowired
	private SeckillUserMapper seckillUserMapper;

	@Override
	public ResponseBean<Object> register(RegisterRequest registerRequest) throws Exception {

		// 获取秘钥
		String cacheKey = RedissonUtil.getRedisKey(NeptuneConstant.REDIS_RSAKEY_SESSION,
				registerRequest.getToken());
		AesKeyBean aesKeyBean = redisRepository.get(cacheKey, AesKeyBean.class);

		if (null == aesKeyBean){
			return ResponseBean.fail(null, "操作超时");
		}

		if (StringUtils.isBlank(registerRequest.getUsername())){
			return ResponseBean.fail(null, "username not empty");
		} else if (StringUtils.isBlank(registerRequest.getNickname())){
			return ResponseBean.fail(null, "nickname not empty");
		} else if (StringUtils.isBlank(registerRequest.getPassword())){
			return ResponseBean.fail(null, "password not empty");
		} else if (StringUtils.isBlank(registerRequest.getEmail())){
			return ResponseBean.fail(null, "email not empty");
		} else if (StringUtils.isBlank(registerRequest.getMobile())){
			return ResponseBean.fail(null, "mobile not empty");
		}

		SeckillUserExample seckillUserExample = new SeckillUserExample();
		seckillUserExample.createCriteria().andUsernameEqualTo(registerRequest.getUsername());
		if (seckillUserMapper.countByExample(seckillUserExample) > 0){
			return ResponseBean.fail(null, "username exist");
		}
		// 密码还原
		String password = SecurityKeyUtil.decryptAES(registerRequest.getPassword(),
				aesKeyBean.getAseKey());
		String encryptPwd = AESUtils.encrypt(password, NeptuneConstant.PASSWORD_ENCRYPT_KEY);

		seckillUserMapper.insertSelective(this.convertBean(registerRequest, encryptPwd));
		return ResponseBean.success(null, "register success");
	}

	private SeckillUser convertBean(RegisterRequest registerRequest, String password) {
		SeckillUser seckillUser = new SeckillUser();
		seckillUser.setId(RandomUtils.createRandomUUID());
		seckillUser.setUsername(registerRequest.getUsername());
		seckillUser.setPassword(password);
		seckillUser.setNickName(registerRequest.getNickname());
		seckillUser.setMobile(registerRequest.getMobile());
		seckillUser.setEmail(registerRequest.getEmail());
		return seckillUser;
	}

}
